/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Ripper
 */
public class RegisterPanel extends ClientPanel implements ActionListener
{
    private String[] attributesToRegister = {"first name", "last name", "username", "password","re-type password","e-mail"};
    private JTextField[] attributeInputs;
    
    private JButton registerButton;
    private JButton cancelButton;
    
    public RegisterPanel(ClientWindow parent)
    {
        super(parent);
        setLayout(new BorderLayout());
        
        JPanel amashedPanel = new JPanel();
        {
            FlowLayout fl = new FlowLayout();
            fl.setAlignment(FlowLayout.CENTER);
            fl.setVgap(200);
            amashedPanel.setLayout(fl);
            
            attributeInputs = new JTextField[attributesToRegister.length];
            
            GridLayout gl = new GridLayout(attributesToRegister.length + 2, 2);
            gl.setHgap(2);
            gl.setVgap(2);
            
            JPanel attributePanel = new JPanel(gl);
            {
                for(int a = 0; a < attributesToRegister.length; a++)
                {
                    String s = attributesToRegister[a];

                    JLabel label = new JLabel(s+":  ");
                    attributePanel.add(label);

                    attributeInputs[a] = new JTextField("");
                    attributePanel.add(attributeInputs[a]);
                }
                
                attributePanel.add(new JLabel(""));
                registerButton = new JButton("register");
                registerButton.addActionListener(this);
                attributePanel.add(registerButton);
            }
            amashedPanel.add(attributePanel);
        }
        add(amashedPanel, BorderLayout.CENTER);
        
        JPanel bottomPanel = new JPanel(new BorderLayout());
        {
            cancelButton = new JButton("cancel registration");
            cancelButton.addActionListener(this);
            bottomPanel.add(cancelButton, BorderLayout.EAST);
        }
        add(bottomPanel, BorderLayout.SOUTH);
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == cancelButton)
        {
            getClientWindow().gotoLoginScreen();
        }
        else if(e.getSource() == registerButton)
        {
            boolean go_on = true;
            for(JTextField jt: attributeInputs)
            {
                if(jt.getText().length() < 1)
                {
                    go_on = false;
                    getClientWindow().displayErrorMessage("Not all fields have been filled out.");
                }
            }
            
            if(go_on)
            {
                if(attributeInputs[3].getText().equals(attributeInputs[4].getText()))
                {
                    String[] data = new String[5];
                    data[0] = attributeInputs[0].getText();
                    data[1] = attributeInputs[1].getText();
                    data[2] = attributeInputs[2].getText();
                    data[3] = attributeInputs[3].getText();
                    data[4] = attributeInputs[5].getText();
                    
                    getClientWindow().attemptRegistration(data);
                }
                else
                {
                    getClientWindow().displayErrorMessage("passwords do not match.");
                }
            }
            
            //TODO: register. (first check if the 2 passwords are the same)
        }
    }
}
